<?php 
	define(SEPARATE, '/');

	echo '<div class="floattop"><div class="itemtitle"><h3>视频列表</h3><ul class="tab1">';
	echo '<li><a href="admin.php?action=video&operation=add" style="text-decoration:none;"><span style="color:#666;">添加</span></a></li><li class="current"><a href="admin.php?action=video&operation=manage" style="text-decoration:none;"><span style="color:#666;">管理</span></a></li>';
	echo '</ul></div></div>';

	if($operation=='add'){
		$max_size = ini_get("post_max_size");
		$cates = C::t('home_videocate')->get_all_cate();

		echo '<form method="post" id="video" action="admin.php?action=video&operation=set" enctype="multipart/form-data">';
		echo '<table>';
		echo '<tr><th>视频图片：</th><td><input type="file" name="v_image">文件不超过'.$max_size.'</td></tr>';
		echo '<tr><th>视频分类：</th><td><select name="cate_id"><option value="0">请选择分类</option>';
		foreach ($cates as $key => $value) {
			echo '<option value="'.$value['cate_id'].'">'.$value['cate_name'].'</option>';
		}
		echo '</select></td></tr>';
		echo '<tr><th>视频名称：</th><td><input type="text" name="v_name"></td></tr>';
		echo '<tr><th>周&nbsp;&nbsp;观看：</th><td><input type="text" name="view_week">虚拟</td></tr>';
		echo '<tr><th>月&nbsp;&nbsp;观看：</th><td><input type="text" name="view_month">虚拟</td></tr>';
		echo '<tr><th>视频价格：</th><td><input type="text" name="v_price" value="0.00"></td></tr>';
		echo '<tr><th>优酷地址：</th><td><input type="text" name="v_url" value="http://"></td></tr>';
		echo '<tr><th>视频排序：</th><td><input type="text" name="v_sort" value="255"></td></tr>';
		echo '<tr><td colapsn="2"><input type="submit" class="btn" id="submit_cardsubmit" name="cardsubmit" title="按 Enter 键可随时提交您的修改" value="提交"></td></tr>';
		echo '</form>';
	}

	if($operation=='set'){
		$flag = true;
		if($_FILES['v_image']['name'] == ''){
			$flag = false;
		}
		foreach ($_POST as $key => $value) {
			if($value == ''){
				$flag = false;
			}
		}
		if(!$flag){
			echo '<script type="text/Javascript">alert("请填写完整信息!");window.history.back();</script>';
		}else{
			$fileInfo = pathinfo($_FILES['v_image']['name']);
			$fileInfo['extension'] = strtolower($fileInfo['extension']);
			$_FILES['v_image']['name'] = time().rand(100,999).'.'.$fileInfo['extension'];	
			if($fileInfo['extension']=='exe'){//非法操作
				echo '<script type="text/Javascript">alert("上传非法文件!");window.history.back();</script>';
			}
			if($fileInfo['extension'] != 'jpg' && $fileInfo['extension'] != 'gif' && $fileInfo['extension'] != 'png'){
				echo '<script type="text/Javascript">alert("请上传jpg,png,gif格式的图片!");window.history.back();</script>';
			}
			$new_name = getcwd().SEPARATE.'static'.SEPARATE.'image'.SEPARATE.'video'.SEPARATE.$_FILES['v_image']['name'];
			$re = move_uploaded_file($_FILES['v_image']['tmp_name'], $new_name);
			$data['v_image'] = 'static'.SEPARATE.'image'.SEPARATE.'video'.SEPARATE.$_FILES['v_image']['name'];//文件路径,存库用

			$data['v_name'] = trim($_POST['v_name']);
			$data['cate_id'] = trim($_POST['cate_id']);
			$data['view_week'] = trim($_POST['view_week']);
			$data['view_month'] = trim($_POST['view_month']);
			$data['v_price'] = trim($_POST['v_price']);
			$data['v_url'] = trim($_POST['v_url']);
			$data['v_sort'] = trim($_POST['v_sort']);
			$data['add_time'] = time();
			$data1 = array('%s,%s,%s,%d,%d,%f,%s,%d,%d');
			
			C::t('home_video')->add_video($data,$data1);
			echo '<script type="text/Javascript">alert("视频添加成功!");';
			echo 'window.location="'.ADMINSCRIPT.'?action=video&operation=manage";';
			echo '</script>';
		}
	} elseif($operation=='manage' || empty($operation)){
		$totalRow = C::t('home_video')->get_video_nums();
		$pageSize = 10;
		$totalPage = ceil($totalRow / $pageSize);
		if($_GET['p'] == ''){
			$currentPage = 1;
		}else{
			$currentPage = $_GET['p'];
		}
		$start = ($currentPage - 1) * $pageSize;
		$re = C::t('home_video')->get_all_video(0, $start, $pageSize);

		$str = '';
		for ($i=1; $i <= $totalPage; $i++) { 
			if($currentPage != $i)
				$str .= '<a href="admin.php?action=video&operation=manage&p='.$i.'"><span style="margin:10px;">'.$i.'</span></a>';
			else
				$str .= '<span style="margin:10px;">'.$i.'</span>';
		}

		if(!empty($re)){
			echo '<table border="3">';
			echo '<tr>';
			echo '<th>视频图片</th>';
			echo '<th>视频名称</th>';
			echo '<th>分类名称</th>';
			echo '<th>周观看(虚拟)</th>';
			echo '<th>周观看(实际)</th>';
			echo '<th>月观看(虚拟)</th>';
			echo '<th>月观看(实际)</th>';
			echo '<th>视频价格</th>';
			echo '<th>优酷地址</th>';
			echo '<th>排序</th>';
			echo '<th>操作</th>';
			foreach($re as $val){
				echo '<tr>';
				echo '<td align="center" style="padding:10px;"><img src="'.$val['v_image'].'" style="width:50px;height:50px;"></td>';
				echo '<td align="center" style="padding:15px;">'.$val['v_name'].'</td>';
				echo '<td align="center" style="padding:15px;">'.$val['cate_name'].'</td>';
				echo '<td align="center" style="padding:15px;">'.$val['view_week'].'</td>';
				echo '<td align="center" style="padding:15px;">'.$val['view_week_true'].'</td>';
				echo '<td align="center" style="padding:15px;">'.$val['view_month'].'</td>';
				echo '<td align="center" style="padding:15px;">'.$val['view_month_true'].'</td>';
				echo '<td align="center" style="padding:15px;">'.$val['v_price'].'</td>';
				echo '<td align="center" style="padding:15px;">'.$val['v_url'].'</td>';
				echo '<td align="center" style="padding:15px;">'.$val['v_sort'].'</td>';
				echo '<td align="center" style="padding:15px;"><a href="admin.php?action=video&operation=del&id='.$val['v_id'].'" style="color:red;">删除</a>';
				echo ' | ';
				echo '<a href="admin.php?action=video&operation=edit&id='.$val['v_id'].'">编辑</a></td>';
				echo '</tr>';
			}
			echo '<tr><td colspan="11" align="center">'.$str.'</td></tr></table>';
		}
	}elseif($operation == 'del' && $_GET['id'] != ''){
		if(!is_numeric($_GET['id'])){
			echo '<script type="text/javascript">alert("非法参数");</script>';
		}else{
			$info = C::t('home_video')->get_video_by_id($_GET['id']);
			if(empty($info)){
				echo '<script type="text/javascript">alert("非法参数");</script>';
			}else{
				C::t('home_video')->del_video($_GET['id']);
				echo '<script type="text/javascript">alert("删除成功");';
				echo 'window.location="'.ADMINSCRIPT.'?action=video&operation=manage";';
				echo '</script>';
			}
		}
	}elseif($operation == 'edit' && $_GET['id'] != ''){
		if(!is_numeric($_GET['id'])){
			echo '<script type="text/javascript">alert("非法参数");</script>';
		}else{
			$info = C::t('home_video')->get_video_by_id($_GET['id']);
			if(empty($info)){
				echo '<script type="text/javascript">alert("非法参数");</script>';
			}else{
				$max_size = ini_get("post_max_size");
				$cates = C::t('home_videocate')->get_all_cate();

				echo '<form method="post" id="video" action="admin.php?action=video&operation=checkEdit&id='.$_GET['id'].'" enctype="multipart/form-data">';
				echo '<table>';
				echo '<tr><th>视频图片：</th><td><input type="file" name="v_image"><a href="http://'.$_SERVER['HTTP_HOST'].'/'.$info['v_image'].'" target="_blank">查看</a>文件不超过'.$max_size.'</td></tr>';
				echo '<input type="hidden" name="v_image" value="'.$info['v_image'].'">';
				echo '<tr><th>视频分类：</th><td><select name="cate_id"><option value="0">请选择分类</option>';
				foreach ($cates as $key => $value) {
					if($value['cate_id'] == $info['cate_id']){
						echo '<option value="'.$value['cate_id'].'" selected>'.$value['cate_name'].'</option>';
					}else{
						echo '<option value="'.$value['cate_id'].'">'.$value['cate_name'].'</option>';
					}
				}
				echo '</select></td></tr>';
				echo '<tr><th>视频名称：</th><td><input type="text" name="v_name" value="'.$info['v_name'].'"></td></tr>';
				echo '<tr><th>周&nbsp;&nbsp;观看：</th><td><input type="text" name="view_week" value="'.$info['view_week'].'">虚拟</td></tr>';
				echo '<tr><th>月&nbsp;&nbsp;观看：</th><td><input type="text" name="view_month" value="'.$info['view_month'].'">虚拟</td></tr>';
				echo '<tr><th>视频价格：</th><td><input type="text" name="v_price" value="0.00" value="'.$info['v_price'].'"></td></tr>';
				echo '<tr><th>优酷地址：</th><td><input type="text" name="v_url" value="'.$info['v_url'].'"></td></tr>';
				echo '<tr><th>视频排序：</th><td><input type="text" name="v_sort" value="'.$info['v_sort'].'"></td></tr>';
				echo '<tr><td colapsn="2"><input type="submit" class="btn" id="submit_cardsubmit" name="cardsubmit" title="按 Enter 键可随时提交您的修改" value="提交"></td></tr>';
				echo '</form>';
			}
		}
	}elseif($operation == 'checkEdit' && $_GET['id'] != ''){
		if(!is_numeric($_GET['id'])){
			echo '<script type="text/javascript">alert("非法参数");</script>';
		}else{
			$info = C::t('home_video')->get_video_by_id($_GET['id']);
			if(empty($info)){
				echo '<script type="text/javascript">alert("非法参数");</script>';
			}else{
				$flag = true;
				foreach ($_POST as $key => $value) {
					if($value == ''){
						$flag = false;
					}
				}
				if(!$flag){
					echo '<script type="text/Javascript">alert("请填写完整信息!");window.history.back();</script>';
				}else{
					if(!empty($_FILES['v_image']['name'])){
						$fileInfo = pathinfo($_FILES['v_image']['name']);
						$fileInfo['extension'] = strtolower($fileInfo['extension']);
						$_FILES['v_image']['name'] = time().rand(100,999).'.'.$fileInfo['extension'];	
						if($fileInfo['extension']=='exe'){//非法操作
							echo '<script type="text/Javascript">alert("上传非法文件!");window.history.back();</script>';
						}
						if($fileInfo['extension'] != 'jpg' && $fileInfo['extension'] != 'gif' && $fileInfo['extension'] != 'png'){
							echo '<script type="text/Javascript">alert("请上传jpg,png,gif格式的图片!");window.history.back();</script>';
						}
						$new_name = getcwd().SEPARATE.'static'.SEPARATE.'image'.SEPARATE.'video'.SEPARATE.$_FILES['v_image']['name'];
						$re = move_uploaded_file($_FILES['v_image']['tmp_name'], $new_name);
						$data['v_image'] = 'static'.SEPARATE.'image'.SEPARATE.'video'.SEPARATE.$_FILES['v_image']['name'];//文件路径,存库用
					}else{
						$data['v_image'] = $_POST['v_image'];
					}

					$data['v_name'] = trim($_POST['v_name']);
					$data['cate_id'] = trim($_POST['cate_id']);
					$data['view_week'] = trim($_POST['view_week']);
					$data['view_month'] = trim($_POST['view_month']);
					$data['v_price'] = trim($_POST['v_price']);
					$data['v_url'] = trim($_POST['v_url']);
					$data['v_sort'] = trim($_POST['v_sort']);
					$data1 = array('%s','%s','%s','%d','%d','%f','%s','%d');
					
					C::t('home_video')->update_video($data,$data1,$_GET['id']);
					echo '<script type="text/Javascript">alert("视频修改成功!");';
					echo 'window.location="'.ADMINSCRIPT.'?action=video&operation=manage";';
					echo '</script>';
				}
			}
		}
	}

?>